package com.module.home.ui.healthDataDetails

import androidx.lifecycle.LiveData
import androidx.lifecycle.asLiveData
import com.common.base.app.viewmodel.BaseViewModel
import com.common.app.utls.TimeUtils.getDayRangeLegacy
import com.common.app.utls.TimeUtils.getFirstDayOfMonthLegacy
import com.common.app.utls.TimeUtils.getLastDayLegacy
import com.module.ble.db.table.UserBloodOxygen
import com.module.ble.db.table.UserBodyTemperature
import com.module.ble.db.table.UserHeartRate
import com.module.ble.repo.BloodOxygenRepo
import com.module.ble.repo.BodyTemperatureRepo
import com.module.ble.repo.HeartRateRepo
import com.module.ble.utils.CommonCusHealthType
import com.module.ble.utils.CycleType
import java.util.Calendar

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2024/11/18
 * <p>
 * 描述：\
 * <p>
 * 修订历史：
 */
class HealthDataDetailsViewModel : BaseViewModel() {
    
    var allHeartRates: LiveData<List<UserHeartRate>>? = null

    var allBloodOxygens: LiveData<List<UserBloodOxygen>>? = null

    var allUserBodyTemperatures: LiveData<List<UserBodyTemperature>>? = null

    fun initCycleData(mType: Int = 0,mCycle: CycleType = CycleType.DAY) {
        val dateRange = getDateRange(mCycle)
        when (mType) {
            CommonCusHealthType.HEARTRATE.value-> allHeartRates = HeartRateRepo.instance.findByTimeRangeFlow(dateRange.first,dateRange.second)?.asLiveData()
            CommonCusHealthType.BLOODOXYGEN.value-> allBloodOxygens= BloodOxygenRepo.instance.findByTimeRangeFlow(dateRange.first,dateRange.second)?.asLiveData()
            CommonCusHealthType.BODYTEMP.value-> allUserBodyTemperatures = BodyTemperatureRepo.instance.findByTimeRangeFlow(dateRange.first,dateRange.second)?.asLiveData()
        }

    }

    // 获取日期范围 - 基于周期类型
    private fun getDateRange(cycleType: CycleType): Pair<Long, Long> {
        return when (cycleType) {
            CycleType.WEEK -> {
                val calendar = Calendar.getInstance()
                // 设置为周一
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
                calendar.set(Calendar.HOUR_OF_DAY, 0)
                calendar.set(Calendar.MINUTE, 0)
                calendar.set(Calendar.SECOND, 0)
                calendar.set(Calendar.MILLISECOND, 0)
                val weekStart = getDayRangeLegacy(calendar.timeInMillis).first

                // 移动到周末
                calendar.add(Calendar.DAY_OF_WEEK, 6)
                val weekEnd = getDayRangeLegacy(calendar.timeInMillis).second
                Pair(weekStart, weekEnd)
            }
            CycleType.MONTH -> {
                val firstDay = getDayRangeLegacy(getFirstDayOfMonthLegacy()).first
                val lastDay = getDayRangeLegacy(getLastDayLegacy()).second
                Pair(firstDay, lastDay)
            }
            CycleType.DAY -> {
                val dayRange = getDayRangeLegacy(System.currentTimeMillis())
                Pair(dayRange.first, dayRange.second)
            }
        }
    }

    fun refreshDBAllData() {
            HeartRateRepo.instance.refreshData()
            BloodOxygenRepo.instance.refreshData()
            BodyTemperatureRepo.instance.refreshData()
    }

}